using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace TheLogWatcher
{
    public partial class MainWindow : Form
    {
        /// <summary>
        ///     Adds the currently loaded file to history and reorders the list
        ///     so that the last loaded one is always the first entry in list.
        /// </summary>
        /// <param name="filename"></param>
        private void HistoryOrganizer_AddEntry(string filename)
        {
            if (sets.History == null || sets.History.Length == 0)
            {
                sets.History = new string[1];
            }
            // The current loaded log file is now the first item
            // in the history overview. When this item already
            // exists, it won't be added again but its position change.
            if (sets.History[0] != null)
            {
                int foundPos = -1;
                for (int i = 0; i < sets.History.Length; i++)
                {
                    if (sets.History[i].Equals(filename, StringComparison.CurrentCultureIgnoreCase))
                    {
                        foundPos = i;
                        break;
                    }
                }

                // This log file wasn't loaded before
                if (foundPos == -1)
                {
                    // Add one entry
                    string[] obj = sets.History;
                    Array.Resize<string>(ref obj, obj.Length + 1);

                    for (int i = obj.Length - 1; i > 0; i--)
                    {
                        obj[i] = obj[i - 1];
                    }
                    sets.History = obj;
                }
                else
                {
                    for (int i = foundPos; i > 0; i--)
                    {
                        sets.History[i] = sets.History[i - 1];
                    }
                }
            }
            sets.History[0] = filename;
            sets.Save();

            RefreshHistoryEntries();
        }

        /// <summary>
        ///     Removes the currently loaded file from history menu.
        /// </summary>
        /// <param name="filename"></param>
        private void HistoryOrganizer_RemoveEntry(string filename)
        {
            if (sets.History != null)
            {
                ArrayList entries = new ArrayList();

                // Add all entries to ArrayList not matching the current filename
                for (int i = 0; i < sets.History.Length; i++)
                {
                    if (!sets.History[i].Equals(filename, StringComparison.CurrentCultureIgnoreCase))
                    {
                        entries.Add(sets.History[i]);
                    }
                }

                // Convert ArrayList to string array and save to history
                sets.History = (string[]) entries.ToArray(typeof(string));
                sets.Save();

                // Rebuild history menu
                RefreshHistoryEntries();
            }
        }

        /// <summary>
        ///     Clears the history menu items and builds them again.
        /// </summary>
        private void RefreshHistoryEntries()
        {
            // HISTORY
            if (sets.History != null)
            {
                // When refreshing and history entries have changed, kill entries that are too much
                string[] obj = sets.History;
                if (obj.Length > sets.MaximalHistoryEntries)
                {
                    Array.Resize<string>(ref obj, sets.MaximalHistoryEntries);
                }

                ToolStripMenuItem historyMenu = (ToolStripMenuItem)menuStripMainWindow.Items["historyToolStripMenuItem"];
                historyMenu.DropDownItems.Clear();
                for (int i = 0; i < obj.Length; i++)
                {
                    ToolStripMenuItem item = new ToolStripMenuItem();
                    item.Tag = obj[i];
                    if (sets.FilenamesWithoutPath)
                    {
                        item.Text = obj[i].Substring(obj[i].LastIndexOf('\\') + 1);
                        item.ToolTipText = obj[i];
                    }
                    else
                    {
                        item.Text = obj[i];
                    }
                    item.Click += new EventHandler(toolStripHistoryItem_Click);
                    historyMenu.DropDownItems.Add(item);
                }
            }
        }
    }
}
